package ui;

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.HeadlessException;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSeparator;

/**
 * The main window that is responsible for displaying the vast majority
 * of the program.  Various panels and forms of any size can be displayed
 * in its scroll pane.
 * 
 * @author Aaron Chen
 * @version 3/6/2014
 */
@SuppressWarnings("serial")
public class MainPage extends JFrame {

  /** The main display panel. */
  private JPanel my_content_pane;
  
  /** The main scroll pane. */
  private JScrollPane my_scroll_pane;
  
  
  /** The login window instance. */
  private JFrame my_login_page;
  
  
  private JMenuItem my_paper_form;
  
  private JMenuItem my_review_form;
  
  private JMenuItem my_rec_form;

  
  /**
   * Launch the application.
   * @param the_args not used
   */
  public static void main(final String[] the_args) {
    EventQueue.invokeLater(new Runnable() {
      public void run() {
        try {
          final MainPage frame = new MainPage();
        } catch (final HeadlessException e) {
          e.printStackTrace();
        }
      }
    });
  }

  /**
   * Create the frame.
   */
  public MainPage() {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(200, 100, 750, 700);
    my_content_pane = new JPanel();
    my_content_pane.setBorder(null);
    my_content_pane.setLayout(new BorderLayout(0, 0));
    setContentPane(my_content_pane);
    my_content_pane.add(createJMenuBar(), BorderLayout.NORTH);
    
    my_scroll_pane = new JScrollPane(new UserPanel());
    my_scroll_pane.getVerticalScrollBar().setUnitIncrement(Standards.SCROLL_BAR_INCREMENT);
    my_scroll_pane.getHorizontalScrollBar().setUnitIncrement(Standards.SCROLL_BAR_INCREMENT);
    my_scroll_pane.setAlignmentX(CENTER_ALIGNMENT);
    my_content_pane.add(my_scroll_pane, BorderLayout.CENTER);

    setVisible(true);
    setLocationRelativeTo(null);
    my_login_page = new LoginPage(this);
    my_login_page.setVisible(true);
    my_login_page.setLocationRelativeTo(this);
  }
  
  /**
   * Creates the JMenuBar for the main window.
   * @return The menu bar for the main window.
   */
  private JMenuBar createJMenuBar() {
    final JMenuBar menu_bar = new JMenuBar();
    
    menu_bar.add(createFileJMenu());
    menu_bar.add(createViewJMenu());
    menu_bar.add(createHelpJMenu());
    
    return menu_bar;
  }
  
  /**
   * Creates the file drop down menu.
   * @return The file menu.
   */
  private JMenu createFileJMenu() {
    final JMenu mn_file = new JMenu("File");
    mn_file.setMnemonic('F');
    
    final JMenuItem mntm_log_in = new JMenuItem("Log in");
    mntm_log_in.setMnemonic('I');
    mntm_log_in.addActionListener(new ActionListener() {
      /** The action to be performed.
       * When the menu item "Log In" is clicked, the login window is opened. */
      @Override
      public void actionPerformed(final ActionEvent the_e) {
        my_login_page.setLocationRelativeTo(null);
        my_login_page.setVisible(true);
      }
    });
    
    final JMenuItem mntm_log_out = new JMenuItem("Log out");
    mntm_log_out.setMnemonic('O');
    mntm_log_out.addActionListener(new ActionListener() {
      /** The action to be performed.
       * When the menu item "Log Out" is clicked, the user is logged out of the
       * system. */
      @Override
      public void actionPerformed(final ActionEvent the_e) {
        //TODO Log Out action
      }
    });
    
    final JMenuItem mntm_exit = new JMenuItem("Exit");
    mntm_exit.setMnemonic('E');
    mntm_exit.addActionListener(new ActionListener() {
      /** The action to be performed.
       * When the menu item "Exit" is clicked, the program terminates. */
      @Override
      public void actionPerformed(final ActionEvent the_e) {
        dispose();
        System.exit(0);
      }
    });
    
    mn_file.add(mntm_log_in);
    mn_file.add(mntm_log_out);
    mn_file.add(new JSeparator());
    mn_file.add(mntm_exit);
    
    return mn_file;
  }
  
  public void setRoles(List<String> the_list){
    String role = the_list.get(6);
    
    JLabel my_label = new JLabel();
    my_label.setText("Welcome " + the_list.get(1) +" "+ the_list.get(2) +", please select your form from the View menu.");
    my_label.setVisible(true);
    
    my_scroll_pane.setViewportView(my_label);
    
//    if(role.equals(""))
//    {
//      paperForm.setEnabled(true);
//      reviewForm.setEnabled(false);
//      recForm.setEnabled(false);
//    }
//    else 
      if(role.equals("2"))
    {
      my_paper_form.setEnabled(true);
      my_review_form.setEnabled(false);
      my_rec_form.setEnabled(true);
    }
    else if(role.equals("4"))
    {
      my_paper_form.setEnabled(true);
      my_review_form.setEnabled(true);
      my_rec_form.setEnabled(false);
    } else {      //PC and Author can only access manuscript submission form for now. 
        my_paper_form.setEnabled(true);
        my_review_form.setEnabled(false);
        my_rec_form.setEnabled(false);
    }
    
  }
  
  /**
   * Creates the view menu, which allows the user to select various panels and forms
   * to view.
   * @return The view menu.
   */
  private JMenu createViewJMenu() {
    final JMenu mn_view = new JMenu("View");
    mn_view.setMnemonic('V');
    
    my_paper_form = new JMenuItem("Manuscript Submission Form");
    my_paper_form.addActionListener(new ActionListener() {
      /** The action to be performed.
       * When selected, a new Manuscript Submission Form is opened. */
      @Override
      public void actionPerformed(final ActionEvent the_e) {
        my_scroll_pane.setViewportView(new ManuscriptSubmissionPanel());
      }
    });
    
    my_review_form = new JMenuItem("Review Submission Form");
    my_review_form.addActionListener(new ActionListener() {
      /** The action to be performed.
       * When selected, a new Review Submission Form is opened. */
      @Override
      public void actionPerformed(final ActionEvent the_e) {
        my_scroll_pane.setViewportView(new ReviewSubmissionPanel());
      }
    });
    
    my_rec_form = new JMenuItem("Recommendation Form");
    my_rec_form.addActionListener(new ActionListener() {
      /** The action to be performed.
       * When selected, a new Recommendation Form is opened. */
      @Override
      public void actionPerformed(final ActionEvent the_e) {
        my_scroll_pane.setViewportView(new RecommendationPanel());
      }
    });

    mn_view.add(my_paper_form);
    mn_view.add(my_review_form);
    mn_view.add(my_rec_form);
    
    return mn_view;
  }
  
  /**
   * Creates the help menu.
   * @return The help menu.
   */
  private JMenu createHelpJMenu() {
    final JMenu mn_help = new JMenu("Help");
    mn_help.setMnemonic('H');
    
    final JMenuItem mntm_about = new JMenuItem("About");
    mntm_about.setMnemonic('A');
    
    mntm_about.addActionListener(new ActionListener() {
      /** The action to be performed.
       * When clicked on, the help message dialogue appears. */
      @Override
      public void actionPerformed(final ActionEvent the_e) {
        JOptionPane.showMessageDialog(MainPage.this, "Welcome to the Peer Review System.\n" 
            + "Please login and then choose your role options.\n"
            + "If you have problems, please contact tech support.");
      }
    });
    
    mn_help.add(mntm_about);
    
    return mn_help;
  }

}
